
from models import Promotion
import pdb
# Create your views here.
from django.contrib.auth.models import User 
from models import Item
from cart.models import BuyItemForm
from django.template.response import TemplateResponse
from store.models import Showcase, Collection
from pdb import Pdb
def index(request):
    
    return TemplateResponse(request,'store/index2.html',{})

def itemf(request,id):
    if request.session.get('cart'):
        from cart.views import Carthasitem
        item=Carthasitem.objects.filter(cart__pk=request.session.get('cart'), item__pk=id)
        if len(item)>0:
            quantity=item[0].number
        else:
            quantity=0
    else: 
        quantity=0
    item=Artical.objects.get(pk=id)
    show_cases=item.show_cases
    #This field is a hidden field, it contains id of the item that the buy form represents
    form=BuyItemForm(initial={'quantity':quantity})
    return TemplateResponse(request,'store/itemdetails.html',{'item':item, 'show_cases':show_cases,'form':form})
from models import Artical
def iteml(request):
    articals=Artical.objects.all()
    items_show_case_l=[]
#   show cases for each item
    show_cases=[]
    x=0
    for artical in articals:
        
        #=======================================================================
        # show_cases contain QuerySets for each item. Sometimes those QuerySets are empty
        #=======================================================================
        if artical.main_show_case:
            items_show_case_l.append((articals[x],artical.main_show_case))
        else:
            items_show_case_l.append((articals[x],[]))
        x+=1
    #Pdb().set_trace()
    
    return TemplateResponse(request,'store/itemlist3.html',{'items':items_show_case_l})

    
def addtocart(request):
    return TemplateResponse(request, 'cart/cart.html',{})

def render_collection(request,collection):
    collection=Collection.objects.get(pk=collection)
    show_case=collection.show_cases.filter(collectionhasshowcase__main=True)
    collection_has_items=collection.items.all()
    
    return TemplateResponse(request,'store/collection.html', {'show_case':show_case,'collection_has_items':collection_has_items})


def promotion(request, promotion):
    '''
    Input: 
        request
        promotion: pk of the promotion.
    Output:
        items in the promotion
        the promotion itself
        the form associated with this promotion
    '''
    promotion=Promotion.objects.get(pk=promotion)
    item_set=promotion.artical_set.all()
    from store.models import PromotionForm
    promotion_form=PromotionForm(promotion)
    return TemplateResponse(request,'store/promotion.html',{'item_set':item_set,'promotion':promotion,'promotion_form':promotion_form})

def process_promotion(request):
    pass
def category(request, category):
    from models import Category
    category=Category.objects.get(pk=category)
    items=category.items.filter(categoryhasitem__category=category)
    pdb=Pdb()
    item_main_show_case_tl=[]
    for item in items:
        main_show_case=item.main_show_case
        if main_show_case:
            item_main_show_case_tl.append((item,main_show_case))
        else:
            item_main_show_case_tl.append((item,[]))
    return TemplateResponse(request,'store/itemlist3.html',{'items':item_main_show_case_tl})
    
    

    